<template>
  <div class="app-container">
    <el-form style="margin-left: -5px" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="托盘编号" prop="trayNo">
        <el-input
          v-model="queryParams.trayNo"
          placeholder="请输入托盘编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: -25px" label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="托盘状态" clearable style="width: 120px">
          <el-option
            v-for="dict in dict.type.tray_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="trayList" >
      <el-table-column label="序号" align="center" prop="trayNo" width="60" />
      <el-table-column label="RFID编码" align="center" prop="rfidNo">
        <template slot-scope="scope">
          <span>{{ scope.row.rfidNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="早到库位号" align="center" prop="zdKwh" width="100"/>
      <el-table-column label="乘客姓名" align="center" prop="ckName" width="100"/>
      <el-table-column label="行李重量" align="center" prop="ckXlZl" width="100"/>
      <el-table-column label="航班号" align="center" prop="flight" width="100"/>
      <el-table-column label="中转航班号" align="center" prop="zzFlight" width="100"/>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tray_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改托盘对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="托盘编号" prop="trayNo">
          <el-input v-model="form.trayNo" placeholder="请输入托盘编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  展示rfid具体信息  -->
<!--    <el-dialog :visible.sync="showRfid" width="500px" append-to-body>
      <el-descriptions :title="rfidTitle" style="margin-top: -20px" :column="3">
        <el-descriptions-item label="乘客姓名">{{ this.rfidInfo.ckName }}</el-descriptions-item>
        <el-descriptions-item label="行李重量">{{ this.rfidInfo.ckXlZl }}kg</el-descriptions-item>
        <el-descriptions-item label="航班">{{ this.rfidInfo.flight }}</el-descriptions-item>
        <el-descriptions-item v-if="this.rfidInfo.zzFlight"
                              label="中转航班"
        >{{ this.rfidInfo.zzFlight }}
        </el-descriptions-item>
        <el-descriptions-item v-if="this.rfidInfo.zdKwh"
                              label="库位号(早到)"
        >{{ this.rfidInfo.zdKwh }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>-->

  </div>
</template>

<script>
import { listTray, getTray, updateTray, addTray, delTray } from "@/api/ground/tray";

export default {
  name: "Tray",
  dicts: ['tray_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 托盘表格数据
      trayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trayNo: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        trayNo: [
          { required: true, message: "托盘编码", trigger: "blur" }
        ]
      },
      //展示rfid信息
      showRfid:false,
      rfidTitle:'',
      rfidInfo:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询托盘列表 */
    getList() {
      this.loading = true;
      listTray(this.queryParams).then(response => {
        this.trayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加托盘";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      if(row.status == '1'){
        this.openWarning();
        return;
      }
      this.reset();
      const trayId = row.trayId
      getTray(trayId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改托盘";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.trayId != undefined) {
            updateTray(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTray(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.status == '1'){
        this.openWarning();
        return;
      }
      const trayId = row.trayId;
      this.$modal.confirm('是否确认删除托盘编号为"' + row.rfidNo + '"的数据项？').then(function() {
        return delTray(trayId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    //警告消息
    openWarning() {
      this.$message({
        showClose: true,
        message: '使用中的托盘不允许操作！',
        type: 'warning'
      });
    },

  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-descriptions__title {
  font-size: 14px;
  font-weight: bold;
}
</style>
